const path = require('path')
const resolve = (dir) => path.resolve(__dirname, dir)
const aliasConfig = require('./alias.config')

// https://cli.vuejs.org/zh/
const CONFIG = {
  // publicPath: '/', // 默认/，自定义变量process.env.VUE_APP_PUBLIC_PATH
  // outputDir: 'dist', // 默认dist
  assetsDir: 'static',
  productionSourceMap: false,
  // https://cli.vuejs.org/zh/guide/css.html#css-modules

  css: {
    // extract: true,
    sourceMap: false,
    loaderOptions: {
      less:{
        // http://lesscss.org/usage/#less-options-strict-units `Global Variables`
        // `primary` is global variables fields name
        globalVars: {
          'color-primary': '#1989fa'
        }
      }
    }
  },

  lintOnSave: false,

  // 本地开发代理服务器
  // https://github.com/chimurai/http-proxy-middleware
  devServer: {
    // host: "127.0.0.1", // 本机
    port: process.env.port, // 端口
    // https: false,
    // hotOnly: false, // 热更新
    // open: true, // 是否打开浏览器
    // 让浏览器 overlay 同时显示警告和错误
    overlay: {
      warnings: false,
      errors: false
    },
    proxy: {
      [process.env.VUE_APP_BASE_API]: {
        target: process.env.VUE_APP_BASE_API_PROXY_URL,
        ws: false, // 是否启用websockets
        changeOrigin: true,
        pathRewrite: {
          [`^${process.env.VUE_APP_BASE_API}`]: ''
        }
      }
    }
  },
  configureWebpack(config) {
    config.resolve.alias = {
      ...config.resolve.alias,
      ...aliasConfig.resolve.alias
    }
    if (process.env.NODE_ENV === 'production') {
      config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true
    }
  },
  chainWebpack(config) {
    // 移除 prefetch、preload 插件
    config.plugins.delete('prefetch')
    config.plugins.delete('preload')

    config.plugin('html').tap(args => {
      args[0].minify = false
      args[0].title = process.env.VUE_APP_TITLE
      return args
    })

    if (process.env.use_analyzer) { // 分析
      config.plugin('webpack-bundle-analyzer')
        .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
    }

    // SVG
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config.when(process.env.NODE_ENV === 'production', config => {
      // config
      //   .plugin('ScriptExtHtmlWebpackPlugin')
      //   .after('html')
      //   .use('script-ext-html-webpack-plugin', [{
      //     // `runtime` 必须与 runtimeChunk 名相同，默认是 `runtime`
      //     inline: /runtime\..*\.js$/
      //   }])
      //   .end()
      config
        .optimization.splitChunks({
          chunks: 'all',
          cacheGroups: {
            libs: {
              name: 'chunk-libs',
              test: /[\\/]node_modules[\\/]/,
              priority: 10,
              chunks: 'initial' // 只打包最初依赖的第三方包
            },
            // elementUI: {
            //   name: 'chunk-elementUI', // 将elementUI拆分成单个包
            //   priority: 20, // 重量需要大于libs或app，否则会被打包到libs或app中
            //   test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // 指定包
            // },
            commons: {
              name: 'chunk-commons',
              test: resolve('src/components'), // 公共组件单独打包
              minChunks: 3, // 最小为3个组件
              priority: 5,
              reuseExistingChunk: true
            }
          }
        })
      // https://webpack.js.org/configuration/optimization/#optimizationruntimechunk
      config.optimization.runtimeChunk('single')
    })
  }
}

module.exports = CONFIG
